1694B - Paranoid String - CodeForces Solution


constructive algorithms greedy *1200

Please click on ads to support us..

Python Code:

t=int(input())
for _ in range(t):
    n=int(input())
    st=input()
    ans=n
    for i in range(n-1,-1,-1):
        if st[i]=='1':
            if i!=0 and st[i-1]=='0':
                ans+=i
        else:
            if i>0 and st[i-1]=='1':
                ans+=i
    print(ans)


C++ Code:

#include <bits/stdc++.h>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);cout.setf(ios::fixed);cout.precision(0);
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
int main()
{
    optimizar_io
    ll tt;
    cin>>tt;
    while(tt--)
    {
        ll n;
        cin>>n;
        string a;
        cin>>a;
        ll cont=0;
        for(int i=1; i<n; i++)
            if(a[i]!=a[i-1])
                cont+=i;
        cout<<cont+n<<"\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

810B - Summer sell-off
84A - Toy Army
185A - Plant
1749A - Cowardly Rooks
1749C - Number Game
1749B - Death's Blessing
1749D - Counting Arrays
1447B - Numbers Box
1594D - The Number of Imposters
984B - Minesweeper
837A - Text Volume
1566C - MAX-MEX Cut
1546A - AquaMoon and Two Arrays
897B - Chtholly's request
1363B - Subsequence Hate
437B - The Child and Set
1256B - Minimize the Permutation
733B - Parade
172A - Phone Code
148D - Bag of mice
421A - Pasha and Hamsters
1393A - Rainbow Dash Fluttershy and Chess Coloring
980E - The Number Games
219B - Special Offer Super Price 999 Bourles
560B - Gerald is into Art
322B - Ciel and Flowers
801B - Valued Keys
975C - Valhalla Siege
518B - Tanya and Postcard
514B - Han Solo and Lazer Gun